package br.com.tccallan.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import br.com.tccallan.dao.DAO;
import br.com.tccallan.entity.Cd;

public class CdController {
	
	private Cd cd;
	private List<Cd> cdList;

	public CdController() {
	}
	public Cd getCd() {
		return cd;
	}


	public void setCd(Cd cd) {
		this.cd = cd;
	}

	public List<Cd> getCdList() {
		return cdList;
	}
	public void setCdList(List<Cd> cdList) {
		this.cdList = cdList;
	}
	public void cadastrarCd() throws Exception {
		try {
			cd = new Cd();
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("cadastrarCd.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void salvar() throws Exception {
		DAO.getInstance().saveOrUpdate(cd);
		cd = new Cd();
	}


	public void carregar() throws IOException {
		FacesContext context = FacesContext.getCurrentInstance();
		cd = (Cd) context.getExternalContext().getRequestMap()
				.get("list");
		FacesContext.getCurrentInstance().getExternalContext()
				.redirect("cadastrarCd.jsp");
	}

	@SuppressWarnings("unchecked")
	public List<Cd> listarCd() {
		try {
			cdList = new ArrayList<Cd>();
			cdList = DAO.getInstance().list(Cd.class, "titulo");
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("listarCd.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cdList;
	}

	
}
